package com.starlight.easybuyprictice.service;

import com.starlight.easybuyprictice.entity.Token;
import org.springframework.stereotype.Service;

/**
 * @Author yfy2001
 * @date 2022/11/28 21:33
 */
@Service
public class TokenManager {
    private final RedisService redisService;

    public TokenManager(RedisService redisService) {
        this.redisService = redisService;
    }
    public Token createToken(long id,Token.Type type){
        Token token = new Token(id, type);
        redisService.set(token.getUuid(), token.getValue(),
                type == Token.Type.TT_DEVICE ? 3600 * 24 : 3600);
        return token;
    }

    public boolean checkToken(long id,Token.Type type) {
        return checkToken(new Token(id,type));
    }
    public boolean checkToken (String tokenValue) {
        try {
            return checkToken(new Token(tokenValue));
        }catch (NumberFormatException e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean checkToken(Token token){
        return token.getValue().equals(redisService.get(token.getUuid()));
    }
    public void deleteToken(Token token){
        deleteToken(token.getUuid());
    }
    public void deleteToken(String uuid){
        redisService.delete(uuid);
    }


}
